package com.light.springboot.base.websocket;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.light.springboot.base.websocket.ov.MessageVO;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

@Component
@ServerEndpoint("/webSocket")
public class WebSocket {
 
  private Session session;

  private static ConcurrentHashMap<String, WebSocket> webSockets = new ConcurrentHashMap<>();
//  private static CopyOnWriteArraySet<WebSocket> webSockets = new CopyOnWriteArraySet<>();

  private MessageVO messageVO = new MessageVO();
 
  @OnOpen
  public void onOpen(Session session) {
    this.session = session;
    webSockets.put(session.getId(),this);
//    webSockets.add(this);

    messageVO.setType(1);
    messageVO.setUserNum(webSockets.size());
    messageVO.setMessage("有新的连接");
 
    ObjectMapper mapper = new ObjectMapper();
 
    String Json = "";
    try {
      Json = mapper.writeValueAsString(messageVO);
    } catch (Exception ex) {
//      log.error(ex.getMessage());
    }
 
//    this.sendMessage(Json);
//    log.info("【websocket消息】有新的连接, 总数:{}", webSockets.size());
  }
 
 
  @OnClose
  public void onClose() {
    webSockets.remove(this);
 
    messageVO.setType(2);
    messageVO.setUserNum(webSockets.size());
    messageVO.setMessage("有用户离开");
 
    ObjectMapper mapper = new ObjectMapper();
 
    String Json = "";
    try {
      Json = mapper.writeValueAsString(messageVO);
    } catch (Exception ex) {
//      log.error(ex.getMessage());
    }
 
//    this.sendMessage(Json);
 
 
//    log.info("【websocket消息】连接断开, 总数:{}", webSockets.size());
  }
 
  @OnMessage
  public void onMessage(Session session, String message) {

    String[] split = message.split(",");
    messageVO.setType(3);
    messageVO.setUserNum(webSockets.size());
    messageVO.setToUserId(split[1]);
    messageVO.setMessage(message);
 
    ObjectMapper mapper = new ObjectMapper();
 
    String Json = "";
    try {
      Json = mapper.writeValueAsString(messageVO);
    } catch (Exception ex) {
//      log.error(ex.getMessage());
    }
 
//    this.sendMessage(Json);
    this.sendMessage(messageVO);

//    log.info("【websocket消息】收到客户端发来的消息:{}", message);
  }
 
  public void sendMessage(MessageVO vo) {
//  public void sendMessage(String message) {
    WebSocket webSocket = webSockets.get(vo.getToUserId());
    ObjectMapper mapper = new ObjectMapper();
    try {
      webSocket.session.getBasicRemote().sendText(mapper.writeValueAsString(vo));
    } catch (IOException e) {
      e.printStackTrace();
    }
    /*for (Map.Entry<String, WebSocket> entry : webSockets.entrySet()) {
//    for (WebSocket webSocket : webSockets) {
//      log.info("【websocket消息】广播消息, message={}", message);
      try {
        WebSocket webSocket = entry.getValue();
        webSocket.session.getBasicRemote().sendText(message);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }*/
  }
}
